//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package mm.chenchen.common.utils;

import org.springframework.util.CollectionUtils;

import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

public final class ConvertUtil {
    private static final char[] HEX_CHAR = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};

    public static byte[] uuidToByte(UUID uuid) {
        ByteArrayOutputStream ba = new ByteArrayOutputStream(16);
        DataOutputStream da = new DataOutputStream(ba);

        try {
            da.writeLong(uuid.getMostSignificantBits());
            da.writeLong(uuid.getLeastSignificantBits());
        } catch (IOException var4) {
            var4.printStackTrace();
        }

        return ba.toByteArray();
    }

    public static byte[] getUUIDByte() {
        UUID uuid = UUID.randomUUID();
        ByteArrayOutputStream ba = new ByteArrayOutputStream(16);
        DataOutputStream da = new DataOutputStream(ba);

        try {
            da.writeLong(uuid.getMostSignificantBits());
            da.writeLong(uuid.getLeastSignificantBits());
        } catch (IOException var4) {
            var4.printStackTrace();
        }

        return ba.toByteArray();
    }

    public static String byteToHex(byte[] bytes) {
        char[] buf = new char[bytes.length * 2];
        int i = 0;
        byte[] var3 = bytes;
        int var4 = bytes.length;

        for(int var5 = 0; var5 < var4; ++var5) {
            byte b = var3[var5];
            buf[i++] = HEX_CHAR[b >>> 4 & 15];
            buf[i++] = HEX_CHAR[b & 15];
        }

        return new String(buf);
    }

    private static byte toByte(char c) {
        return (byte)"0123456789abcdef".indexOf(c);
    }

    public static byte[] hexToByte(String hex) {
        int len = hex.length() / 2;
        byte[] result = new byte[len];
        char[] achar = hex.toLowerCase().toCharArray();

        for(int i = 0; i < len; ++i) {
            int pos = i * 2;
            result[i] = (byte)(toByte(achar[pos]) << 4 | toByte(achar[pos + 1]));
        }

        return result;
    }

    public static <F, T> T convert(F from, Class<T> clazz) {
        if (null == from) {
            return null;
        } else {
            Object to = null;

            try {
                to = clazz.newInstance();
            } catch (InstantiationException var15) {
                return null;
            } catch (IllegalAccessException var16) {
                return null;
            }

            Class<?> fromClass = from.getClass();
            Field[] fromFields = fromClass.getDeclaredFields();
            Field[] var5 = fromFields;
            int var6 = fromFields.length;

            for(int var7 = 0; var7 < var6; ++var7) {
                Field field = var5[var7];

                try {
                    Field toField = clazz.getDeclaredField(field.getName());
                    field.setAccessible(true);
                    toField.setAccessible(true);
                    String fieldType = field.getType().toString();
                    String toFieldType = toField.getType().toString();
                    if (Objects.equals(fieldType, toFieldType)) {
                        toField.set(to, field.get(from));
                    }
                } catch (NoSuchFieldException var12) {
                } catch (IllegalArgumentException var13) {
                } catch (IllegalAccessException var14) {
                }
            }

           // return to;
            return null;
        }
    }

    public static <F, T> List<T> covertList(List<F> from, Class<T> clazz) {
        List<T> resultList = new ArrayList();
        if (!CollectionUtils.isEmpty(from)) {
            Iterator var3 = from.iterator();

            while(var3.hasNext()) {
                //F f = var3.next();
                //resultList.add(convert(f, clazz));
            }
        }

        return resultList;
    }

    public static <F, T> T convertSuper(F from, Class<T> clazz) {
        if (null == from) {
            return null;
        } else {
            Object to = null;

            try {
                to = clazz.newInstance();
            } catch (InstantiationException var15) {
                return null;
            } catch (IllegalAccessException var16) {
                return null;
            }

            List<Field> fieldList = new ArrayList();
            Class<?> fromClass = from.getClass();
            if (fromClass != Object.class) {
                Class<?> superClass = fromClass.getSuperclass();
                if (superClass != Object.class) {
                    Field[] superFields = superClass.getDeclaredFields();
                    fieldList.addAll(Arrays.asList(superFields));
                }
            }

            Field[] fromFields = fromClass.getDeclaredFields();
            fieldList.addAll(Arrays.asList(fromFields));
            Iterator var7 = fieldList.iterator();

            while(var7.hasNext()) {
                Field field = (Field)var7.next();

                try {
                    Field toField = clazz.getDeclaredField(field.getName());
                    field.setAccessible(true);
                    toField.setAccessible(true);
                    String fieldType = field.getType().toString();
                    String toFieldType = toField.getType().toString();
                    if (Objects.equals(fieldType, toFieldType)) {
                        toField.set(to, field.get(from));
                    }
                } catch (NoSuchFieldException var12) {
                } catch (IllegalArgumentException var13) {
                } catch (IllegalAccessException var14) {
                }
            }

            //return to;
            return null;
        }
    }

    public static <K, T> Map<K, T> listToMap(List<T> list, Function<? super T, ? extends K> keyMapper) {
        return (Map)(CollectionUtils.isEmpty(list) ? new HashMap() : (Map)list.stream().collect(Collectors.toMap(keyMapper, (t) -> {
            return t;
        })));
    }

    private ConvertUtil() {
    }
}
